tinyMCEPopup.requireLangPack();

var ed;

var FrediumPluginsDialog = {
    init: function () {
        ed = tinyMCEPopup.editor;
        tinyMCEPopup.resizeToInnerSize();
        var f = document.forms[0];
        var fe = ed.selection.getNode();
        f.hidPropertyList.value = fe.title;
        pl = fe.title;
        if (pl != "") {
            pl = tinyMCEPopup.editor.plugins.frediumplugins._parse(pl);
            if (pl && pl.pluginid && pl.pluginid != "") {
                f.ddlPlugins.value = pl.pluginid;
            }
        }
        f.btnUpdate.click(); // Trigger a hidden update button that reloads the form with the fields for the selected plugin
    },

    insert: function () {
        var f = document.forms[0];
        ed.execCommand('mceRepaint'); //new!
        tinyMCEPopup.restoreSelection();
        if (f.ddlPlugins[f.ddlPlugins.selectedIndex].value != "-1" && (f.ddlTemplates && f.ddlTemplates.length > 0) || f.hidTemplatablePlugin.value == "false") {
            img = ed.dom.create('img', {
                src: "/" + tinyMCEPopup.getWindowArg("plugin_url") + '/img/trans.gif',
                'class': 'mceItemFrediumPlugin'
            });
            img.title = serializeParameters();
            img.width = "100";
            img.height = "100";
            ed.selection.setNode(img); //new!
        }
        tinyMCEPopup.close();
    }
};

function serializeParameters() {
    var d = document, f = d.forms[0], s = '';
    s = "pluginid:'" + f.ddlPlugins[f.ddlPlugins.selectedIndex].value + "'";
    s += ",classname:'" + f.hidClassName.value + "'";
    s += ",assemblyname:'" + f.hidAssemblyName.value + "'";
    if (f.ddlTemplates)
        s += ",templateid:'" + f.ddlTemplates[f.ddlTemplates.selectedIndex].value + "'";
    // Regular text properties
    var pnlPlugin = document.getElementById("pnlPlugin");
    var inputs = pnlPlugin.getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        var property = "";
        var value = "";
        if (inputs[i].type == "text") {
            property = inputs[i].getAttribute("property");
            value = inputs[i].value;
            value = value.split("%").join("%25");
            value = value.split("\\").join("%5c");
            value = value.split("'").join("%27");
            value = value.split("\"").join("%22");
            value = value.split("<").join("%3c");
            value = value.split(">").join("%3e");
        }
        else {
            if (inputs[i].type == "checkbox") {
                property = inputs[i].parentNode.getAttribute("property");
                value = inputs[i].checked ? "true" : "false";
            }
        }
        s += "," + property + ":'" + value + "'";
    }
    // Textarea properties
    var txts = pnlPlugin.getElementsByTagName("textarea");
    for (var i = 0; i < txts.length; i++) {
        var property = txts[i].getAttribute("property");
        var value = txts[i].value;
        value = value.split("%").join("%25");
        value = value.split("\\").join("%5c");
        value = value.split("'").join("%27");
        value = value.split("\"").join("%22");
        value = value.split("<").join("%3c");
        value = value.split(">").join("%3e");
        s += "," + property + ":'" + value + "'";
    }

    var ddls = pnlPlugin.getElementsByTagName("select");
    for (var i = 0; i < ddls.length; i++) {
        var property = ddls[i].getAttribute("property");
        var value = ddls[i][ddls[i].selectedIndex].value;
        value = value.split("%").join("%25");
        value = value.split("\\").join("%5c");
        value = value.split("'").join("%27");
        value = value.split("\"").join("%22");
        value = value.split("<").join("%3c");
        value = value.split(">").join("%3e");
        s += "," + property + ":'" + value + "'";
    }
    return s;
}

tinyMCEPopup.onInit.add(FrediumPluginsDialog.init, FrediumPluginsDialog);
